package com.htgd.gardenexpo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.codec.binary.Base64;
import java.util.Base64;

import org.springframework.web.bind.annotation.*;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.Cipher;
@RestController
@Api(tags = "RSA工具")
@Slf4j
@RequestMapping("/util/rsa")
public class RSAUtil {
//    public static String decrypt(String encryptedText, String privateKeyStr) throws Exception {
////        byte[] privateKeyBytes = Base64.decodeBase64(privateKeyStr);
//
//        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyStr);
//        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyBytes);
//
//        // 创建 PKCS#8 格式的私钥规范
//        KeyFactory kf = KeyFactory.getInstance("RSA");
//        PrivateKey privateKey = kf.generatePrivate(spec);
//
//        Cipher cipher = Cipher.getInstance("RSA");
//        cipher.init(Cipher.DECRYPT_MODE, privateKey);
//        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
//        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
//        return new String(decryptedBytes, "UTF-8");
//    }


    @ApiOperation("解密")
    @PostMapping("/decryption")
    public static String decryption(@RequestBody String encryptedText) throws Exception {
        JSONObject jsonObject = JSON.parseObject(encryptedText);
        String key = jsonObject.getString("encryption");
        String privateKeyStr ="MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA1lTW+K3qOpisjC7eP+ER4m6Ms03mJLTK/sC4GK0W+znM2LeUIJY7lU8zw2wBzZEP4/3zcoavXlT09pA3i652iwIDAQABAkBy+HGUVh5mLk2oqN95Xq8sSmbZK/k/SWf2vcRDIF5KhhxqkOnsT3UoikF3TDl9SPp5oTdSZugocaPAx/9gaZRRAiEA8ZoCE51vEe49wbqMwLvsfN8E7YnS5jrEPyK84+USzbkCIQDjGsjIAXbj63rYeWztgPtXbRQ4ypbi8aQfa9NEcWQoYwIhAIPVJsbvRDlDh/1bPwy4Pp4Qz86SQzuP26Ab43hzeiQ5AiEAh83+fsuRYt3vyf9iiev1YORTkVpkPDWvpTJ/VZBRCm0CIE2SaP3LqnyONHSknaBRTNOD337hR57Shqg+1wMrSMRQ";

        String decryptedText = RSAUtils.privateDecrypt(key, RSAUtils.getPrivateKey(privateKeyStr));
        log.info("RSA工具-解密:{}",decryptedText);
        return decryptedText;
    }

    @ApiOperation("解密字符串")
    @PostMapping("/decryptionString")
    public static String decryptionString(String encryptedText) throws Exception {
        String privateKeyStr ="MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA1lTW+K3qOpisjC7eP+ER4m6Ms03mJLTK/sC4GK0W+znM2LeUIJY7lU8zw2wBzZEP4/3zcoavXlT09pA3i652iwIDAQABAkBy+HGUVh5mLk2oqN95Xq8sSmbZK/k/SWf2vcRDIF5KhhxqkOnsT3UoikF3TDl9SPp5oTdSZugocaPAx/9gaZRRAiEA8ZoCE51vEe49wbqMwLvsfN8E7YnS5jrEPyK84+USzbkCIQDjGsjIAXbj63rYeWztgPtXbRQ4ypbi8aQfa9NEcWQoYwIhAIPVJsbvRDlDh/1bPwy4Pp4Qz86SQzuP26Ab43hzeiQ5AiEAh83+fsuRYt3vyf9iiev1YORTkVpkPDWvpTJ/VZBRCm0CIE2SaP3LqnyONHSknaBRTNOD337hR57Shqg+1wMrSMRQ";
        String decryptedText = RSAUtils.privateDecrypt(encryptedText, RSAUtils.getPrivateKey(privateKeyStr));
        log.info("RSA工具-解密字符串:{}",decryptedText);
        return decryptedText;
    }
}
